package com.tongfusoft.document.resource.service.impl;

import java.sql.Timestamp;
import java.util.Date;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.tongfusoft.da.template.service.impl.DataTemplateServiceImpl;
import com.tongfusoft.document.entity.Resource;
import com.tongfusoft.document.resource.service.IResourceService;
import com.tongfusoft.spring.web.core.util.ControllerUtils;
import com.tongfusoft.utils.UIDGeneratorService;

@Service("resourceService")
public class ResourceServiceImpl extends DataTemplateServiceImpl implements IResourceService {

	@Override
	public void save(Map<String, String> resMap) {
		try{
		Resource resource = new Resource();
		resource.setResContent(resMap.get(ControllerUtils.URL));
		if (StringUtils.isNotEmpty(resMap.get(ControllerUtils.URL))) {
			if (this.selectOne("selectResource", resource) != null) {

				resource = (Resource) this.selectOne("selectResource", resource);
				resource.setResDesc(resMap.get(ControllerUtils.DESC));
				resource.setResName(resMap.get(ControllerUtils.NAME));
				resource.setLastUpdDate(new Timestamp(new Date().getTime()));

				super.update("updateResource", resource);

			} else {

				resource.setResId(UIDGeneratorService.getUUID());
				resource.setResDesc(resMap.get(ControllerUtils.DESC));
				resource.setResName(resMap.get(ControllerUtils.NAME));
				resource.setCreatedDate(new Timestamp(new Date().getTime()));
				resource.setLastUpdDate(new Timestamp(new Date().getTime()));
				super.insert("addResource", resource);
			}
		}
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}
